<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use App\Models\UserInfo;
use App\Models\GetUserLogs;
use App\Models\Share;
use Carbon\Carbon;
use \Cache;
use \Schema;
use App\Common\Tools\Push;

class UserController extends Controller
{
    public function test()
    {
        $app = new \Qbhy\TtMicroApp\TtMicroApp([
            'access_key' => 'tt3ecd4870a1f7dbb0',
            'secret_key' => '4880224bb36e457637418715447bed6fa314b570',
            'debug' => true,
        ]);

        var_dump($app->auth->session('23e08f0b35aec944'));
    }
    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function show()
    {

        $user = User::with('info')->where('id', user_id())->first();
        return $this->successReturn($user);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function homeInfo($id)
    {
        $user = User::with(['info','requirement'])->where('id', $id)->first();
        return $this->successReturn($user);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function bind(Request $request)
    {
        $this->validate($request, [
            'phone' => 'required|string|unique:users,phone',
        ]);
        $user = User::where('id', user_id())->update(['phone' => $request->phone]);
        $push = new Push;
        $push->send('认证结果通知' , user_id() , ['thing1'=>'绑定手机通知' , 'phrase2'=>'通过' , 'thing3'=>'恭喜,绑定手机成功']);
        return $this->successReturn($user);
    }


    /**
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function share(Request $request)
    {
        $share = Share::where('created_at' , '>' ,Carbon::yesterday())->first();
        if(!empty($share)){
            return $this->errorReturn('已分享');
        }
        Share::create([
            'user_id'=>user_id()
        ]);
        return $this->successReturn('分享成功');
    }

    /**
     * 首页用户列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function homeList(Request $request)
    {
        $requirement = User::where('id', user_id())->first();
        $user_infos = UserInfo::where('user_id', user_id())->first();

        if(empty($user_infos)){
            return $this->errorReturn(['if_new'=>1 , 'msg'=>'信息不全']);
        }

        //获取次数
        $time = GetUserLogs::where('user_id', user_id())->where('day', Carbon::today())->first();
        $see_time = 1;
        $share = Share::where('created_at' , '>' ,Carbon::yesterday())->first();
        if(!empty($share)){
            $see_time += 1;
        }

        //不是会员只有一次
        if ($time >= $see_time && $user_infos->member_expire > Carbon::today()) {
            return $this->errorReturn('次数不足');
        }
        $first_year = $requirement->highest_age ? Carbon::today()->subYears($requirement->highest_age) : null;
        $second_year = $requirement->lowest_age ? Carbon::today()->subYears($requirement->lowest_age) : null;
        $list = UserInfo::with('user')
            ->when($user_infos->sex != 0, function ($query) use ($user_infos) {
                $sex = $user_infos->sex == 1 ? 2 : 1;
                return $query->where('sex', $sex);
            })
//            ->when(!empty($requirement->if_married), function ($query) use ($requirement) {
//                return $query->where('if_married', $requirement->if_married);
//            })
//            ->when(!empty($first_year), function ($query) use ($first_year) {
//                return $query->where('birth_day', '>', $first_year);
//            })
//            ->when(!empty($second_year), function ($query) use ($second_year) {
//                return $query->where('birth_day', '<', $second_year);
//            })
//            ->where('user_id', '!=', user_id())
            ->paginate(7);
        foreach($list as $k=>$v){
            $list[$k]['age'] = $this->getAge(strtotime($v['birth_day']));
        }
        return $this->successReturn($list);
    }

    function getAge($birthday)
    {
//格式化出生时间年月日
        $byear = date('Y', $birthday);
        $bmonth = date('m', $birthday);
        $bday = date('d', $birthday);

        //格式化当前时间年月日
        $tyear = date('Y');
        $tmonth = date('m');
        $tday = date('d');

        //开始计算年龄
        $age = $tyear - $byear;
        if ($bmonth > $tmonth || $bmonth == $tmonth && $bday > $tday) {
            $age--;
        }
        return $age;
    }


    /**
     * 首页用户列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function firstTimeHome(Request $request)
    {
        //获取次数
//        $time = GetUserLogs::where('user_id', user_id())->where('day', Carbon::today())->first();
        $list = UserInfo::with('user')
//            ->when($user_infos->sex != 0, function ($query) use ($user_infos) {
//                $sex = $user_infos->sex == 1 ? 2 : 1;
//                return $query->where('sex', $sex);
//            })
//            ->when(!empty($requirement->if_married), function ($query) use ($requirement) {
//                return $query->where('if_married', $requirement->if_married);
//            })
//            ->when(!empty($first_year), function ($query) use ($first_year) {
//                return $query->where('birth_day', '>', $first_year);
//            })
//            ->when(!empty($second_year), function ($query) use ($second_year) {
//                return $query->where('birth_day', '<', $second_year);
//            })
//            ->where('user_id', '!=', user_id())
            ->limit(7)
            ->get();
        foreach($list as $k=>$v){
            $list[$k]['age'] = $this->getAge(strtotime($v['birth_day']));
        }
        return $this->successReturn($list);
    }

}
